/**
 * @class   DesktopNotifierQObject
 * @brief   
 *
 *
 *
 ****************************************************************************
 * @version $Id: DesktopNotifierQObject.h 2288 2010-04-09 08:33:26Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_DesktopNotifierQObject_H__
 #define __WOSH_DesktopNotifierQObject_H__

#include <woshDefs.h>
#include <string>

#include <QSystemTrayIcon>
#include <QObject>

#include "IDesktopNotifierListener.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _OS_WIN32
# include <core/win32/desktop.h>
#else
# include <core/posix/desktop.h>
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

class DesktopNotifierQObject : public QObject
 {
	Q_OBJECT

	public:
		DesktopNotifierQObject() : QObject(NULL) {
			this->listener = NULL;
			this->trayIcon = NULL;
			this->currX = -1;
			this->currY = -1;
			this->timerId = 0;
			this->timerSpan = 15000;
		 }

		virtual ~DesktopNotifierQObject() { }

	public:

		QSystemTrayIcon* getTrayIcon()		{ return this->trayIcon; }

		void setTrayIcon( QSystemTrayIcon* tray_icon ) {
			if ( this->trayIcon != NULL ) {
				//disconnect..
			 }
			this->trayIcon = tray_icon;
			if ( this->trayIcon == NULL )
				return;
			connect(this->trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
			connect(this->trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
			this->trayIcon->show();
		 }

		void setListener( IDesktopNotifierListener* ptr )	{ this->listener = ptr; }

		void setMouseMoveAccurancy( int seconds ) {
			this->timerSpan = seconds;
			if ( this->timerId != 0 ) {
				monitor(true);
			 }
		 }


	public slots:

		bool showTrayMessage( const std::string& subject, const std::string& text, long showTime, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information ) {
			if ( this->trayIcon == NULL ) return false;
			this->trayIcon->showMessage( subject.c_str(), text.c_str(), icon, showTime );
			return true;
		 }


	public:
		void monitor( bool value ) {
			if ( value ) {
#ifdef _OS_WIN32
				getMouseLocation( this->currX, this->currY ); // init
#else
				///@bug to be implemented on POSIX
				this->currX = this->currY = 0;
#endif
				if ( this->timerId != 0)
					QObject::killTimer( this->timerId );
				this->timerId = QObject::startTimer( this->timerSpan );
			 }
			else if ( this->timerId != 0)
				QObject::killTimer( this->timerId );
		 }

	protected:
	//	virtual void closeEvent(QCloseEvent *event) {
	//	 }

		virtual void timerEvent ( QTimerEvent* ) {
			if ( this->listener == NULL )
				return;
			long x = -1;
			long y = -1;
#ifdef _OS_WIN32
			bool retrieved = getMouseLocation( x, y );
#else
			///@bug to be implemented on POSIX
			bool retrieved = false;
#endif
			if ( !retrieved )
				return;
			if ( this->currX == x && this->currY == y ) {
				this->listener->user_MouseNotMoved();
				return;
			 }
			this->currX = x;
			this->currY = y;
			this->listener->user_MouseMoved();
		 }


	private slots:

		void iconActivated( QSystemTrayIcon::ActivationReason reason ) {
			short mode = 0;
			switch (reason) {
				case QSystemTrayIcon::Context:		mode = -1; break;
				case QSystemTrayIcon::Trigger:		mode = 1; break;
				case QSystemTrayIcon::DoubleClick:	mode = 3; break;
				case QSystemTrayIcon::MiddleClick:	mode = 2; break;
				default: break;
			 }
			if ( this->listener == NULL )
				return;
			this->listener->user_TrayIconClicked(mode);
		}
 
		void messageClicked() {
			this->listener->user_TrayMessageClicked();
		 }

	private:
//		QAction *minimizeAction;
//		QAction *maximizeAction;
//		QAction *restoreAction;
//		QAction *quitAction;
//		QMenu *trayIconMenu;

	protected:
		void createTrayIcon() {
			this->trayIcon = new QSystemTrayIcon(this);
//			this->trayIcon->setContextMenu(trayIconMenu);
//			this->trayIcon->setIcon(this->windowIcon());
			this->trayIcon->setToolTip("WOSH DesktopNotifier");
			setTrayIcon(this->trayIcon);
		 }

	protected:
		QSystemTrayIcon* trayIcon;
		int timerId;
		int timerSpan;

	protected:
		long currX;
		long currY;

		IDesktopNotifierListener* listener;

}; // class def
 
#endif //__WOSH_DesktopNotifierQObject_H__
